package com.ruoyi.school.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.school.domain.PacInternship;
import com.ruoyi.school.domain.Student;
import com.ruoyi.school.service.PacInternshipService;
import com.ruoyi.school.mapper.PacInternshipMapper;
import com.ruoyi.school.utils.CommonUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author jeremylin
 * @description 针对表【pac_internship(见习实习)】的数据库操作Service实现
 * @createDate 2025-06-04 13:17:32
 */
@Service
public class PacInternshipServiceImpl extends ServiceImpl<PacInternshipMapper, PacInternship>
        implements PacInternshipService {

    @Override
    public List<PacInternship> getList(PacInternship pacInternship) {
        List<PacInternship> list = lambdaQuery()
                .eq(Objects.nonNull(pacInternship.getStuId()), PacInternship::getStuId, pacInternship.getStuId())
                .le(Objects.nonNull(pacInternship.getShipTime()), PacInternship::getStartTime, pacInternship.getShipTime())
                .ge(Objects.nonNull(pacInternship.getShipTime()), PacInternship::getEndTime, pacInternship.getShipTime())
                .list();
        return list;
    }

    @Override
    public PacInternship getDetails(PacInternship pacInternship) {
        if (pacInternship.getId() == null) {
            throw new BaseException("id不能为空");
        }
        PacInternship details = lambdaQuery()
                .eq(PacInternship::getId, pacInternship.getId())
                .one();
        return details;
    }

    @Override
    public boolean add(PacInternship pacInternship) {
        validateInternship(pacInternship);

        return save(pacInternship);
    }

    @Override
    public boolean edit(PacInternship pacInternship) {
        validateInternship(pacInternship);
        if (pacInternship.getId() == null) {
            throw new BaseException("id不能为空");
        }
        return updateById(pacInternship);
    }

    @Override
    public boolean delete(PacInternship pacInternship) {
        if (pacInternship.getId() == null) {
            throw new BaseException("id不能为空");
        }

        return removeById(pacInternship.getId());
    }

    private void validateInternship(PacInternship pacInternship) {
        if (pacInternship.getStartTime() == null) {
            throw new BaseException("开始时间不能为空");
        }
        if (pacInternship.getEndTime() == null) {
            throw new BaseException("结束时间不能为空");
        }
        if (pacInternship.getShipAddress() == null) {
            throw new BaseException("实习地点不能为空");
        }
        if (pacInternship.getShipTerm() == null) {
            throw new BaseException("实习学期不能为空");
        }
        if (pacInternship.getShipCont() == null) {
            throw new BaseException("实习内容不能为空");
        }
        if (pacInternship.getShipWeek() == null) {
            throw new BaseException("实习周数不能为空");
        }

        Student student = CommonUtils.getStudent();
        if (student != null) {
            pacInternship.setStuId(Long.valueOf(student.getId()));
        }else{
            throw new BaseException("学生帐号信息异常");
        }
    }
}




